<?php

class modifOeuvreValidation extends Controller {


	function index()
	{
		
		$this->load->database();
		$this->load->library('form_validation');
		$this->load->helper('form');
		$this->load->helper('url');
		$this->load->helper('path');
		
		
		$config['upload_path'] = set_realpath("./image/media/");
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '0';
		$config['max_width']  = '0';
		$config['max_height']  = '0';		
		$this->load->library('upload', $config);
		
		$data;	
		
		
		
		$image;
		$oeuvre;
		$okFile=true;
		
		$this->form_validation->set_rules('nom',  'Nom', 'required');		
		$this->form_validation->set_message('required', 'Vous n\'avez pas renseigner le champs %s ');
				
		if(isset($_FILES['fileImage']) && $_FILES['fileImage']['name']!=""){		
			$okFile=$this->upload->do_upload('fileImage');			
			$data['error'] = $this->upload->display_errors();	
		}
		else $image=($this->input->post('selectImage')) ;
		
		
		if ($this->form_validation->run() && $okFile ){ 
		
		if (isset($_COOKIE['login'])){
		
			$ID_Oeuvre = $this->input->post('ID_Oeuvre');
			
			$this->db->where('oeuvre', $ID_Oeuvre);
			$this->db->delete('expose'); 
		
			
			if(isset($_FILES['fileImage']) && $_FILES['fileImage']['name']!=""){
			$data['upload_data'] =  $this->upload->data();
			$nomFile=$this->db->escape($data['upload_data']['file_name']);
			$sql = "INSERT INTO media (chemin,oeuvre,type,utilisateur)VALUES (".$nomFile.",NULL,'image',".$_COOKIE['login'].")";
			$this->db->query($sql);	
			$image=$this->db->insert_id();}
					
			$data["nom"]=($this->input->post('nom')) ;
			$this->db->set('nom',$this->input->post('nom'));		
			$this->db->set('presentation',$this->input->post('presentation'));
			$this->db->set('descriptif',$this->input->post('descriptif'));
			$this->db->set('auteur',$this->input->post('auteur'));
			$this->db->set('date_creation',$this->input->post('periode'));
			$this->db->set('lieu_creation',$this->input->post('pays'));
			$this->db->set('image_principale',$image);
			
			$this->db->where('ID_Oeuvre',$ID_Oeuvre );
			$this->db->update('oeuvre'); 	
				
		
			foreach( ($this->input->post('selectExposition')) as $exposition):
			if ($exposition != 0){
				$this->db->set('exposition',$exposition);
				$this->db->set('oeuvre',$ID_Oeuvre);
				$this->db->insert('expose'); 
				}
			endforeach;
		
			if($image != $this->input->post('old_image')) {
			
			$this->db->set('oeuvre',$ID_Oeuvre);
			$this->db->where('ID_Media', $image);
			$this->db->update('media'); 
			
			$this->db->set('oeuvre',Null);
			$this->db->where('ID_Media', $this->input->post('old_image'));
			$this->db->update('media'); 
			
			}

		//$data['upload_data'] =  $this->upload->data();
		
			$this->load->view('modifOeuvreValidation',$data);
			}
		}
		
		else {
				
			$data;
		
		$query = $this->db->query("SELECT ID_Exposition, nom FROM exposition where utilisateur ='".$_COOKIE['login']."'");

		foreach ($query->result_array() as $row)
		{
			$data["expositions"][]=array($row['ID_Exposition'],$row['nom']);
		}
		
		$query = $this->db->query("SELECT ID_Media, chemin FROM media where type ='image' && oeuvre is NULL && utilisateur='".$_COOKIE['login']."'");
		
		foreach ($query->result_array() as $row)
		{
			$data["images"][]=array($row["ID_Media"],$row['chemin']);
		}
		
		$id=$this->input->post('selectNomOeuvre');
		
		if($id !=0){
		
		$query = $this->db->query('SELECT nom,presentation,descriptif, auteur,date_creation,lieu_creation,image_principale FROM oeuvre where ID_Oeuvre ='.$id);

		$row = $query->row();
		
			$data["nom"]=$row->nom;
			$data["descriptif"]=$row->descriptif;
			$data["presentation"]=$row->presentation;
			$data["auteur"]=$row->auteur;
			$data["periode"]=$row->date_creation;
			$data["pays"]=$row->lieu_creation;
			$data["id_image_principale"]=$row->image_principale;
			
			$query = $this->db->query('SELECT chemin FROM media where ID_Media ='.$row->image_principale);		
			$data["nom_image_principale"]=$query->row()->chemin;
			
			$query = $this->db->query('SELECT exposition FROM expose where oeuvre ='.$id);
			foreach ($query->result_array() as $row)
			{
				$data["expositionsOeuvre"][]=$row['exposition'];
			}
			
		
			$this->load->view('modifOeuvre',$data);
			
			
		
		}
		else $this->load->view('modifOeuvre',$data);
		
		
		}
		
	}
	
	
	

}
?>
